package com.tianyang.yukuaifu.business.trade.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.alibaba.fastjson.JSONObject;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseTitleActivity;
import com.tianyang.yukuaifu.business.trade.bean.SubAgentStatOutput;
import com.tianyang.yukuaifu.business.trade.contract.SubAgentStatContract;
import com.tianyang.yukuaifu.business.trade.contract.TradeConfig;
import com.tianyang.yukuaifu.business.trade.presenter.SubAgentStatPresenter;
import com.tianyang.yukuaifu.common.utils.DateCheckPopListener;
import com.tianyang.yukuaifu.module.dialog.AchievementDialog;
import com.tianyang.yukuaifu.module.dialog.interfaces.IAchieveListener;
import com.tianyang.yukuaifu.module.roundview.RoundTextView;
import com.tianyang.yukuaifu.module.wheel.interfaces.IWheelDataChangeCallback;
import com.tianyang.yukuaifu.utils.DateUtils;
import com.tianyang.yukuaifu.utils.VerifyUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Description：间推业绩详情
 * Created by xyj on 2024/3/25
 */
public class SubAgentStatActivity extends BaseTitleActivity implements SubAgentStatContract.View {

    @BindView(R.id.filterTv)
    TextView filterTv;
    @BindView(R.id.dayFilterRtv)
    RoundTextView dayFilterRtv;
    @BindView(R.id.monthFilterRtv)
    RoundTextView monthFilterRtv;

    @BindView(R.id.agentNameTv)
    TextView agentNameTv;
    @BindView(R.id.subProfitTv)
    TextView subProfitTv;
    @BindView(R.id.subTradeAmountTv)
    TextView subTradeAmountTv;
    @BindView(R.id.tradeCountTv)
    TextView tradeCountTv;
    @BindView(R.id.directSubAgentNumberTv)
    TextView directSubAgentNumberTv;
    @BindView(R.id.indirectTotalAgentNumberTv)
    TextView indirectTotalAgentNumberTv;
    @BindView(R.id.directSubMerchantNumberTv)
    TextView directSubMerchantNumberTv;
    @BindView(R.id.indirectSubAgentNumberTv)
    TextView indirectSubAgentNumberTv;
    @BindView(R.id.directTotalAgentNumberTv)
    TextView directTotalAgentNumberTv;
    @BindView(R.id.directTotalMerchantNumberTv)
    TextView directTotalMerchantNumberTv;
    @BindView(R.id.indirectTotalMerchantNumberTv)
    TextView indirectTotalMerchantNumberTv;
    @BindView(R.id.indirectSubMerchantNumberTv)
    TextView indirectSubMerchantNumberTv;

    private SubAgentStatContract.Presenter mPresenter;

    private AchievementDialog vAchievementDialog;

    private DateCheckPopListener mDatePopListener;

    private boolean isMonthFilter;

    private String startTime, endTime;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_sub_agent_stat;
    }

    @Override
    protected void initView() {
        super.initView();
        String agentName = getIntent().getStringExtra(TradeConfig.INTENT_AGENT_NAME);
        commonTitle.setTitle(VerifyUtil.checkString(agentName) + "团队业绩详情");
        agentNameTv.setText(VerifyUtil.checkString(agentName));
        String agentPhone = getIntent().getStringExtra(TradeConfig.INTENT_AGENT_PHONE);
        mPresenter = new SubAgentStatPresenter(this, agentName, agentPhone);
    }

    @Override
    protected void initData() {
        super.initData();
        startTime = getIntent().getStringExtra(TradeConfig.INTENT_START_TIME);
        endTime = getIntent().getStringExtra(TradeConfig.INTENT_END_TIME);
        isMonthFilter = getIntent().getBooleanExtra(TradeConfig.INTENT_IS_MONTH_FILTER, false);
        showDayOrMonthView();
        setTimeData(startTime, endTime);
    }

    @OnClick({R.id.filterTv, R.id.dayFilterRtv, R.id.monthFilterRtv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.filterTv:
                if (isMonthFilter) {
                    if (mDatePopListener == null) {
                        mDatePopListener = new DateCheckPopListener((Activity) mContext, true);
                    }
                    mDatePopListener.handleRegionCheck(view, "请选择月份", false, new IWheelDataChangeCallback() {
                        @Override
                        public void wheelDataChangeCallback(String wheelData) {
                            setTimeData(wheelData, wheelData);
                        }
                    });
                } else {
                    if (vAchievementDialog == null) {
                        vAchievementDialog = new AchievementDialog(mContext);
                        vAchievementDialog.setListener(new IAchieveListener() {
                            @Override
                            public void callback(String startTime, String endTime) {
                                setTimeData(startTime, endTime);
                            }
                        });
                    }
                    vAchievementDialog.setTimeData(startTime, endTime);
                    vAchievementDialog.show();
                }
                break;
            case R.id.dayFilterRtv:
                if (isMonthFilter) {
                    isMonthFilter = false;
                    showDayOrMonthView();
                    setTimeData(DateUtils.getTimeAgo(30, "yyyy-MM-dd"), DateUtils.getCurrentTime("yyyy-MM-dd"));
                }
                break;
            case R.id.monthFilterRtv:
                if (!isMonthFilter) {
                    isMonthFilter = true;
                    showDayOrMonthView();
                    setTimeData(DateUtils.getCurrentMonth(), DateUtils.getCurrentMonth());
                }
                break;
        }
    }

    @Override
    public void showSubAgentStatView(SubAgentStatOutput output) {
        if (output != null) {
            JSONObject subAgentDevProfitResult = output.getSubAgentDevProfitResult();
            if (subAgentDevProfitResult != null) {
                if (subAgentDevProfitResult.containsKey("subProfit")) {
                    subProfitTv.setText(VerifyUtil.decimalFormat(subAgentDevProfitResult.getString("subProfit")));
                }
                if (subAgentDevProfitResult.containsKey("subTradeAmount")) {
                    subTradeAmountTv.setText(VerifyUtil.decimalFormat(subAgentDevProfitResult.getString("subTradeAmount")));
                }
                if (subAgentDevProfitResult.containsKey("tradeCount")) {
                    tradeCountTv.setText(String.valueOf(subAgentDevProfitResult.getInteger("tradeCount")));
                }
                if (subAgentDevProfitResult.containsKey("directSubAgentNumber")) {
                    directSubAgentNumberTv.setText(String.valueOf(subAgentDevProfitResult.getInteger("directSubAgentNumber")));
                }
                if (subAgentDevProfitResult.containsKey("directSubMerchantNumber")) {
                    directSubMerchantNumberTv.setText(String.valueOf(subAgentDevProfitResult.getInteger("directSubMerchantNumber")));
                }
                if (subAgentDevProfitResult.containsKey("cumulativeIndirectSubAgentNumber")) {
                    indirectTotalAgentNumberTv.setText(String.valueOf(subAgentDevProfitResult.getInteger("cumulativeIndirectSubAgentNumber")));
                }
                if (subAgentDevProfitResult.containsKey("cumulativeDirectSubAgentNumber")) {
                    directTotalAgentNumberTv.setText(String.valueOf(subAgentDevProfitResult.getInteger("cumulativeDirectSubAgentNumber")));
                }
                if (subAgentDevProfitResult.containsKey("cumulativeDirectSubMerchantNumber")) {
                    directTotalMerchantNumberTv.setText(String.valueOf(subAgentDevProfitResult.getInteger("cumulativeDirectSubMerchantNumber")));
                }
                if (subAgentDevProfitResult.containsKey("cumulativeIndirectSubMerchantNumber")) {
                    indirectTotalMerchantNumberTv.setText(String.valueOf(subAgentDevProfitResult.getInteger("cumulativeIndirectSubMerchantNumber")));
                }
                if (subAgentDevProfitResult.containsKey("indirectSubAgentNumber")) {
                    indirectSubAgentNumberTv.setText(String.valueOf(subAgentDevProfitResult.getInteger("indirectSubAgentNumber")));
                }
                if (subAgentDevProfitResult.containsKey("indirectSubMerchantNumber")) {
                    indirectSubMerchantNumberTv.setText(String.valueOf(subAgentDevProfitResult.getInteger("indirectSubMerchantNumber")));
                }
            }
        }
    }

    private void showDayOrMonthView() {
        if (isMonthFilter) {
            dayFilterRtv.setTextColor(ContextCompat.getColor(mContext, R.color.c_999999));
            dayFilterRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.transparent));
            monthFilterRtv.setTextColor(ContextCompat.getColor(mContext, R.color.black));
            monthFilterRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
        } else {
            monthFilterRtv.setTextColor(ContextCompat.getColor(mContext, R.color.c_999999));
            monthFilterRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.transparent));
            dayFilterRtv.setTextColor(ContextCompat.getColor(mContext, R.color.black));
            dayFilterRtv.getDelegate().setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
        }
    }

    @SuppressLint("SetTextI18n")
    private void setTimeData(String startTimeStr, String endTimeStr) {
        startTime = startTimeStr;
        endTime = endTimeStr;
        if (isMonthFilter) {
            filterTv.setText(startTime.replaceAll("-", "/"));
        } else {
            filterTv.setText(startTime.replaceAll("-", "/") + "-" + endTime.replaceAll("-", "/"));
        }
        mPresenter.subAgentStatHistoryDetail(startTime, endTime);
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(SubAgentStatContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
     }
}
